package com.lvwenguo.controller.supplier;

import cn.itcast.commons.CommonUtils;
import com.lvwenguo.model.SupplierList;
import com.lvwenguo.service.PurchaseServiceInter;
import com.lvwenguo.service.serviceImple.PurchaseServiceImple;
import com.lvwenguo.utils.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//供应商页面具体操作
@WebServlet(urlPatterns = "/SupplierServlet")
public class SupplierServlet extends BaseServlet {
    PurchaseServiceInter purchaseServiceImpol =new PurchaseServiceImple();

    //用于添加供应商信息
    public void AddSupplier(HttpServletRequest request, HttpServletResponse response){
        SupplierList supplierList = CommonUtils.toBean(request.getParameterMap(), SupplierList.class);
        if(supplierList.getSupplier_name().equals("") && supplierList.getContacts_name().equals("") && supplierList.getAddress().equals("") && supplierList.getTel().equals("")){

        request.setAttribute("error","检查信息是否输入完整");
            try {
                request.getRequestDispatcher("/view/supplier/supplier_add.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }}else{
            int i = purchaseServiceImpol.addSupplier(supplierList);
            if(i!=0){
                try {
                    request.getRequestDispatcher("/MenuJumpServlet?method=supplier").forward(request,response);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }else{
                request.setAttribute("error","供应商名称重复");
                try {
                    request.getRequestDispatcher("/view/supplier/supplier_add.jsp").forward(request,response);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }

    }
    //用于模糊查询
    public void selectSupplier(HttpServletRequest request, HttpServletResponse response){
        String suppliername = request.getParameter("suppliername");
        request.setAttribute("supplier", purchaseServiceImpol.selectSupplierMohu("%"+suppliername+"%"));
        try {
            request.getRequestDispatcher("/view/supplier/supplier_list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    //用于修改供应商信息数据回显
    public void updateSupplier(HttpServletRequest request, HttpServletResponse response){

        request.setAttribute("selectSup", purchaseServiceImpol.selectSup(Integer.parseInt(request.getParameter("id"))));
        try {
            request.getRequestDispatcher("/view/supplier/supplier_modify.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    //更新数据
    public void updateSupplierData(HttpServletRequest request, HttpServletResponse response){

        SupplierList supplierList = CommonUtils.toBean(request.getParameterMap(), SupplierList.class);
        System.out.println(supplierList);
        if(supplierList.getSupplier_name().equals("") && supplierList.getContacts_name().equals("") && supplierList.getTel().equals("") && supplierList.getAddress().equals("")){
            request.setAttribute("error","检查信息输入是否完整");
            try {
                request.getRequestDispatcher("/view/supplier/supplier_modify.jsp").forward(request,response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            int i = purchaseServiceImpol.updateSupplier(supplierList);
            if(i!=0){
                try {
                    request.getRequestDispatcher("/MenuJumpServlet?method=supplier").forward(request,response);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else{
                request.setAttribute("error","更新失败");
                try {
                    request.getRequestDispatcher("/view/supplier/supplier_modify.jsp").forward(request,response);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
    //用于删除供应商信息
    public void deleteSupplier(HttpServletRequest request, HttpServletResponse response){
        int i = purchaseServiceImpol.deleteSupplier(Integer.parseInt(request.getParameter("id")));
    if(i!=0){
        try {
            request.getRequestDispatcher("/MenuJumpServlet?method=supplier").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }else{
        request.setAttribute("error","删除失败");
        try {
            request.getRequestDispatcher("/view/supplier/supplier_modify.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    }

}
